const redisPoll = require('redis-connection-pool')
const { REDIS_CONF } = require('../config/db')

module.exports.onLoad = function({ host, port, authPass }) {
    global.redis = redisPoll('redisPool', {
            host: host,
            port: port,
            max_clients: 30,
            perform_checks: false,
            database: 0,
            options: {
                auth_pass: authPass
            }
        })
        // console.log('global.redis', JSON.stringify(global.redis));
}

class CacheUtil {
    get(key) {
        return new Promise(function(resolve, reject) {
            global.redis.get(key, function(err, reply) {
                if (err) {
                    reject(err)
                } else {
                    try {
                        resolve(JSON.parse(reply))
                    } catch (e) {
                        resolve(reply)
                    }
                }
            })
        })
    }

    getString(key) {
        return new Promise(function(resolve, reject) {
            global.redis.get(key, function(err, reply) {
                if (err) {
                    reject(err)
                } else {
                    resolve(reply)
                }
            })
        })
    }

    expire(key, second) {
            return new Promise(function(resolve, reject) {
                global.redis.expire(key, second, function(err) {
                    if (err) {
                        reject(err)
                    } else {
                        resolve(true)
                    }
                })
            })
        }
        // 返回剩余过期时间:当 key 不存在时，返回 -2 。 当 key 存在但没有设置剩余生存时间时，返回 -1 。 否则，以秒为单位，返回 key 的剩余生存时间。
    timeToExpire(key) {
        return new Promise(function(resolve, reject) {
            global.redis.ttl(key, function(err, replies) {
                if (err) {
                    reject(err)
                } else {
                    resolve(replies)
                }
            })
        })
    }

    getList(key) {
        return new Promise(function(resolve, reject) {
            global.redis.get(key, function(err, reply) {
                if (err) {
                    reject(err)
                } else {
                    if (reply === null) {
                        resolve([])
                    } else {
                        resolve(JSON.parse(reply))
                    }
                }
            })
        })
    }

    cleanAll() {
        return new Promise(function(resolve, reject) {
            global.redis.send_command('flushall', [], function(err, reply) {
                if (err) {
                    reject(err)
                } else {
                    resolve(reply)
                }
            })
        })
    }

    setBit(key, offset, value) {
        return new Promise(function(resolve, reject) {
            global.redis.send_command('setbit', [key, offset, value], function(err, reply) {
                if (err) {
                    reject(err)
                } else {
                    resolve(reply)
                }
            })
        })
    }

    getBit(key, offset) {
        return new Promise(function(resolve, reject) {
            global.redis.send_command('getbit', [key, offset], function(err, reply) {
                if (err) {
                    reject(err)
                } else {
                    resolve(reply)
                }
            })
        })
    }

    keys(prefix = '*') {
        return new Promise(function(resolve, reject) {
            global.redis.send_command('keys', [prefix], function(err, reply) {
                if (err) {
                    reject(err)
                } else {
                    resolve(reply)
                }
            })
        })
    }

    set(key, value) {
        return new Promise(function(resolve, reject) {
            global.redis.set(key, JSON.stringify(value), function(err) {
                if (err) {
                    reject(err)
                } else {
                    resolve(true)
                }
            })
        })
    }

    setString(key, value) {
        return new Promise(function(resolve, reject) {
            global.redis.set(key, value, function(err) {
                if (err) {
                    reject(err)
                } else {
                    resolve(true)
                }
            })
        })
    }

    del(key) {
        return new Promise(function(resolve, reject) {
            global.redis.del(key, function(err) {
                if (err) {
                    reject(err)
                } else {
                    resolve(true)
                }
            })
        })
    }
}

module.exports.redis = new CacheUtil()

module.exports.redisUri = `redis://${REDIS_CONF.authPass !== undefined ? ':' + REDIS_CONF.authPass + '@' : ''}${REDIS_CONF.host}:${REDIS_CONF.port}`